#ifndef _KEY_HPP
#define _KEY_HPP

// static TickType_t keyCheckTick = xTaskGetTickCount();

static TaskHandle_t keyCheckHandle;
static TaskHandle_t keyOpH;
static TaskHandle_t keyAutoHandle;


static uint64_t keyPressTime	= 0;
static bool keyInputCheck		= false;
static bool keyStatus_1			= false;
static bool keyStatus_2			= false;
static bool keyStatusAuto		= false;
static bool keyStatusAutoDi		= false;
static bool keyStatusAutoDa		= false;


void keyAddInputChar(char *pStr) {
	strcat(inputCode, pStr);
	// xQueueSend(inputQ, &pStr, 0);
	pageHomeAddInput(pStr);
}


// 手动建
static void keyManualKey(bool status) {
	// 手动键按下
	if(status) {
		beeStart();
		keyPressTime = getTimeUs();
	}
	// 手动键释放
	else {
		beeStop();
		if(getTimeUs() - keyPressTime < stGroup[ST_DI_TIME].intValue * 1000) {
			keyAddInputChar(".");
		}
		else {
			keyAddInputChar("-");
		}
		keyPressTime = 0;
	}
}

static void keyAutoTask() {
	keyStatusAuto = true;
	TickType_t keyCheckTick = xTaskGetTickCount();
	bool keyStatus = false;
	while(keyStatusAutoDi || keyStatusAutoDa) {
		keyStatus = !keyStatus;
		if(keyStatusAutoDi) {
			beeStart();
			keyAddInputChar(".");
			vTaskDelayUntil(&keyCheckTick, stGroup[ST_DI_TIME].intValue / TT);
			beeStop();
			vTaskDelayUntil(&keyCheckTick, stGroup[ST_DI_TIME].intValue / TT / 2 );
		}
		if(keyStatusAutoDa) {
			beeStart();
			keyAddInputChar("-");
			vTaskDelayUntil(&keyCheckTick, stGroup[ST_DI_TIME].intValue / TT * 2.3);
			beeStop();
			vTaskDelayUntil(&keyCheckTick, stGroup[ST_DI_TIME].intValue / TT / 2);
		}
		
	}
	keyStatusAuto = false;
	keyPressTime = 0;
	vTaskDelete(NULL);
}

static void keyAutoStart() {
	if(!keyStatusAuto) {
		xTaskCreatePinnedToCore(keyAutoTask, "keyAutoTask", MEM_UNIT * 2, NULL, 0, &keyAutoHandle, 1);
	}
}

static void key_1() {
	// 手动建 按下
	if(!stGroup[ST_KEY_TYPE].intValue) {
		keyManualKey(keyStatus_1);
	}
	// 自动键
	else {
		if(!stGroup[ST_KEY_INVERT].intValue) {
			if(keyStatus_1) {
				keyAutoStart();
			}
			keyStatusAutoDi = keyStatus_1;
		}
	}
}

static void key_2() {
	if(stGroup[ST_KEY_TYPE].intValue) {
		if(keyStatus_2) {
			keyAutoStart();
		}
		keyStatusAutoDa = keyStatus_2;
	}
}

void keyOpTask() {
	uint32_t bit;
	while(1) {
		xTaskNotifyWait(
			0x00,
			ULONG_MAX,
			&bit,
			portMAX_DELAY
		);
		if((bit & BIT0) != 0) {
			key_1();
		}
		if((bit & BIT1) != 0) {
			key_2();
		}
	}
	vTaskDelete(NULL);
}

static void keyInputCheckTask() {
	TickType_t keyCheckTick = xTaskGetTickCount();
	vTaskDelayUntil(&keyCheckTick, stGroup[ST_DI_TIME].intValue * 1.5 / TT);
	keyAddInputChar(" ");
	vTaskDelayUntil(&keyCheckTick, stGroup[ST_DI_TIME].intValue * 2.5 / TT);
	keyAddInputChar(" ");
	vTaskDelayUntil(&keyCheckTick, 
					(stGroup[ST_AUTO_SEND_TIME].intValue -  (stGroup[ST_DI_TIME].intValue * 4)) / TT);

	if(stGroup[ST_USE_NET].intValue && statusCheck(STATUS_WS_CONNECTED)) {
		if(wsSendOpH != NULL) {
			xTaskNotify(wsSendOpH, BIT0, eSetValueWithOverwrite);
		}
		else {
			log("wsSendOpH = NULL");
		}
	}
	else {
		if(pageCurrent(PAGE_HOME)) {
			pageHomeCleanInput();
		}
	}
	keyInputCheck = false;
	vTaskDelete(NULL);
}



static void keyCheckStart() {
	if(!keyInputCheck) {
		xTaskCreatePinnedToCore(keyInputCheckTask, "keyInputCheck", MEM_UNIT * 4, NULL, 0, &keyCheckHandle, 1);
		// xTaskCreate(keyInputCheckTask, "keyInputCheck", MEM_UNIT * 3, NULL, 0, &keyCheckHandle);
		keyInputCheck = true;
	}
}

static void keyCheckStop() {
	if(keyInputCheck) {
		vTaskDelete(keyCheckHandle);
		keyInputCheck = false;
	}
}


void keyInitTask() {
	gpio_config_t keyConfig_1 =	{
		.intr_type = 0,
		.mode = GPIO_MODE_INPUT,
		.pin_bit_mask = (1ULL << CONFIG_PIN_KEY_1),
		.pull_up_en = 1
	};
	gpio_config(&keyConfig_1);

	gpio_config_t keyConfig_2 = {
		.intr_type = 0,
		.mode = GPIO_MODE_INPUT,
		.pin_bit_mask = (1ULL << CONFIG_PIN_KEY_2),
		.pull_up_en = 1
	};
	gpio_config(&keyConfig_2);

	// 电键操作
	xTaskCreatePinnedToCore(keyOpTask, "keyOpTask", MEM_UNIT * 3, NULL, 0, &keyOpH, 1);
	

	while(1) {
		if(!statusCheck(STATUS_PLAY_CODE)) {
			if(gpio_get_level(CONFIG_PIN_KEY_1) == 0 && !keyStatus_1) {
				keyStatus_1 = true;
				keyCheckStop();
				xTaskNotify(keyOpH, BIT0, eSetValueWithoutOverwrite);
			}
			else if(gpio_get_level(CONFIG_PIN_KEY_1) == 1 && keyStatus_1) {
				keyStatus_1 = false;
				keyCheckStart();
				xTaskNotify(keyOpH, BIT0, eSetValueWithoutOverwrite);
			}

			if(gpio_get_level(CONFIG_PIN_KEY_2) == 0 && !keyStatus_2) {
				keyStatus_2 = true;
				keyCheckStop();
				xTaskNotify(keyOpH, BIT1, eSetValueWithoutOverwrite);
			}
			else if(gpio_get_level(CONFIG_PIN_KEY_2) == 1) {
				if(keyStatus_2) {
					keyStatus_2 = false;
					keyCheckStart();
					xTaskNotify(keyOpH, BIT1, eSetValueWithoutOverwrite);
				}
				if(keyStatusAutoDa) {
					keyStatusAutoDa = false;
				}
			}

			// else if(gpio_get_level(CONFIG_PIN_KEY_2) == 1 && keyStatus_2) {
			// 	keyStatus_2 = false;
			// 	keyCheckStart();
			// 	xTaskNotify(keyOpH, BIT1, eSetValueWithoutOverwrite);
			// }
		}
		DELAY(5);
	}
	vTaskDelete(NULL);
}

#endif